package object2;

/**
 * 多态练习
 */
public class PolyExercise {

    public static void main(String[] args) {

        double d = 13.1;//ok

        long l = (long) d;//ok

        System.out.println(l);//13

        int in = 5;//ok

        //boolean b = (boolean) in;//不对，boolean -> int

        Object obj = "Hello";//ok，编译类型 Object 运行类型 String

        String objStr = (String)obj;//可以 obj原先就指向String类

        System.out.println(objStr);//Hello

        Object objPri = new Integer(5);//可以，向上转型

        //String str = (String) objPri;//不可以 指向Integer的父类引用 转成String

        PolySub polySub = new PolySub();
        System.out.println(polySub.count);//访问属性看编译类型 PolySub 20
        polySub.display();//访问方法先从运行类型开始寻找，找到就返回 20
        PolyBase polyBase = polySub;//向上转型，父类的引用指向子类的对象
        System.out.println(polyBase == polySub);//true 此时polyBase也指向PolySub对象
        System.out.println(polyBase.count);//属性访问看编译类型 PolyBase 10
        polyBase.display();//方法访问从运行类型 PolySub开始寻找 20
    }
}

class PolyBase {

    int count = 10;

    public void display() {
        System.out.println(this.count);
    }
}

class PolySub extends PolyBase {

    int count = 20;

    public void display() {
        System.out.println(this.count);
    }

}